import java.util.LinkedList;
import java.util.Queue;

public class BinaryTree {
    static class TreeNode {
        public char val;
        //存储左孩子节点的引用
        public TreeNode left;
        //存储右孩子节点的引用
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;

        return A;
    }

    //前序遍历
    public void preOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }

    //中序遍历
    public void inOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }

    //后序遍历
    public void posOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        posOrder(root.left);
        posOrder(root.right);
        System.out.print(root.val+" ");
    }

    //求结点个数方法1
    public int countNode = 0;
    public void nodeSize(TreeNode root) {
        if(root == null) {
            return;
        }
        countNode++;
        nodeSize(root.left);
        nodeSize(root.right);
    }

    //求结点个数方法2
    public int nodeSize2(TreeNode root) {
        if(root == null) {
            return 0;
        }

        return nodeSize2(root.left) + nodeSize2(root.right) + 1;
    }

    //求叶子结点个数
    public int getLeaf(TreeNode root) {
        if(root == null) {
            return 0;
        }
        if(root.left == null && root.right == null) {
            return 1;
        }else {
            return getLeaf(root.left) + getLeaf(root.right);
        }
    }

    //求第k层结点数
    public int getKLeve(TreeNode root,int k) {
        if(root == null) {
            return 0;
        }
        if(k == 1) {
            return 1;
        }
        return getKLeve(root.left,k-1) + getKLeve(root.right,k-1);
    }


    //求树的高度
    public int getHight(TreeNode root) {
        if(root == null) {
            return 0;
        }

        int leftH = getHight(root.left);
        int rightH = getHight(root.right);
        return leftH > rightH ? leftH+1 : rightH + 1;
    }

    //检测值为value的元素是否存在
    public TreeNode find(TreeNode root,char value) {
        if(root == null) {
            return null;
        }
        if(root.val == value) {
            return root;
        }

        TreeNode leftval = find(root.left,value);
        if(leftval != null) {
            return leftval;
        }

        TreeNode rightval = find(root.right,value);
        if(rightval != null) {
            return rightval;
        }

        return null;

    }

    public void CengXu(TreeNode root) {
        if(root == null) {
            return;
        }

        Queue<TreeNode> que = new LinkedList<>();
        que.offer(root);

        while( !que.isEmpty()) {
            TreeNode cur = que.poll();
            System.out.print(cur.val+" ");

            if(cur.left != null) {
                que.offer(cur.left);
            }
            if(cur.right != null) {
                que.offer(cur.right);
            }
        }
    }

    public boolean gettrue(TreeNode root) {
        if(root == null) {
            return true;
        }

        Queue<TreeNode> que = new LinkedList<>();
        que.offer(root);

        while(!que.isEmpty()) {
            TreeNode cur = que.poll();
            if(cur != null) {
                que.offer(cur.left);
                que.offer(cur.right);
            }else {
                break;
            }
        }

        while(!que.isEmpty()) {
            TreeNode cur = que.peek();
            if(cur == null) {
                que.poll();
            }else {
                return false;
            }
        }
        return true;
    }
}
